package org.web.standardplatform.client.builder;

import org.web.base.helper.StringHelper;
import org.web.base.helper.excel.ExcelHelper;
import org.web.standardplatform.client.domain.attribute.type.EditType;
import org.web.standardplatform.client.domain.attribute.view.SearchFormFieldDefinition;
import org.web.standardplatform.client.domain.attribute.view.TableFieldDefinition;
import org.web.standardplatform.client.helper.view.rules.TsObjectArrayBuilder;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;


public class MainBuildViewBuilder {


    public static void main(String[] args) {
        try {

            FileOutputStream fileOutputStream = new FileOutputStream("D://build.txt");
            String fileName = "D:\\test33.xlsx";

            FileInputStream fileInputStream = new FileInputStream(fileName);
            int beginNum = 1;
            List<SearchFormFieldDefinition> searchFormFieldDefinitionList = ExcelHelper.parseExcel(fileInputStream, "view", SearchFormFieldDefinition.class, beginNum);
            String result = "export const searchFormConfig: SearchFormFieldDefinition[] = " + buildSearchFormListInfo(searchFormFieldDefinitionList);

            //fileOutputStream.write(result.getBytes(StandardCharsets.UTF_8));

            beginNum = searchFormFieldDefinitionList.size() + 5;
            fileInputStream.close();
            fileInputStream = new FileInputStream(fileName);
            List<TableFieldDefinition> tableFieldDefinitionList = ExcelHelper.parseExcel(fileInputStream, "view", TableFieldDefinition.class, beginNum);
            result = result + "\n\n  export const tableColumnList: MainAttributeInfoInterface[] = " + buildTableFieldListInfo(tableFieldDefinitionList);
            //result = TsObjectArrayBuilder.formatObjectArrayField(result);

            fileOutputStream.write("import { SearchFormFieldDefinition, MainAttributeInfoInterface } from \"@/views/common/ts/Interface\";\n".getBytes(StandardCharsets.UTF_8));
            fileOutputStream.write("import { selectColumnFormatter } from \"@/views/common/ts/CommonFunction\";\n\n".getBytes(StandardCharsets.UTF_8));

            fileOutputStream.write(result.getBytes(StandardCharsets.UTF_8));

            String tableName = MainBuildInitViewBuilder.getTableName(fileName);
            String appName = "web-display-server";
            result = buildAccessURL(tableName, appName);
            fileOutputStream.write(result.getBytes(StandardCharsets.UTF_8));
            fileOutputStream.flush();
            fileOutputStream.close();
            System.out.println(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private static String buildSearchFormListInfo(List<SearchFormFieldDefinition> list) {
        TsObjectArrayBuilder tsObjectArrayBuilder = TsObjectArrayBuilder.create();
        for (SearchFormFieldDefinition searchFormFieldDefinition : list) {
            TsObjectArrayBuilder.ItemBuilder itemBuilder = tsObjectArrayBuilder.addItem();
            itemBuilder.field("queryAttributeName", searchFormFieldDefinition.getQueryAttributeName());
            itemBuilder.field("queryAttributeLabel", searchFormFieldDefinition.getQueryAttributeLabel());
            itemBuilder.field("queryEditType", searchFormFieldDefinition.getQueryEditType().replaceFirst("_", "-"));
            if (searchFormFieldDefinition.getQueryAttributePlaceholder() != null) {
                itemBuilder.field("queryAttributePlaceholder", searchFormFieldDefinition.getQueryAttributePlaceholder());
            }

            if (searchFormFieldDefinition.getQueryRequest() != null) {
                itemBuilder.field("queryRequest", searchFormFieldDefinition.getQueryRequest());
            }

            if (searchFormFieldDefinition.getIsDefaultHidden() != null) {
                itemBuilder.field("isHidden", searchFormFieldDefinition.getIsDefaultHidden());
            }

            if (searchFormFieldDefinition.getRules() != null) {
                itemBuilder.field("rules", searchFormFieldDefinition.getRules(), true);
            }

            if (searchFormFieldDefinition.getEditTypeOperateExtend() != null) {
                if (EditType.select.name().equalsIgnoreCase(searchFormFieldDefinition.getQueryEditType())) {
                    itemBuilder.field("selectOperation", searchFormFieldDefinition.getEditTypeOperateExtend(), true);
                }
            }
            itemBuilder.end();
        }

        return tsObjectArrayBuilder.build();
    }

    private static String buildTableFieldListInfo(List<TableFieldDefinition> list) {
        TsObjectArrayBuilder tsObjectArrayBuilder = TsObjectArrayBuilder.create();
        for (TableFieldDefinition tableFieldDefinition : list) {
            TsObjectArrayBuilder.ItemBuilder itemBuilder = tsObjectArrayBuilder.addItem();
            itemBuilder.field("attributeName", tableFieldDefinition.getAttributeName());
            itemBuilder.field("attributeLabel", tableFieldDefinition.getAttributeLabel());
            itemBuilder.field("editType", tableFieldDefinition.getEditType().replace("_", "-"));
            if (tableFieldDefinition.getAttributeTooltip() != null) {
                itemBuilder.field("attributeTooltip", tableFieldDefinition.getAttributeTooltip());
            }
            if (tableFieldDefinition.getIsSortable() != null) {
                itemBuilder.field("isSortable", tableFieldDefinition.getIsSortable());
            }


            if (tableFieldDefinition.getRules() != null) {
                itemBuilder.field("rules", tableFieldDefinition.getRules(), true);
            }
            if (tableFieldDefinition.getUnInsertable() != null) {
                itemBuilder.field("unInsertable", tableFieldDefinition.getUnInsertable());
            }
            if (tableFieldDefinition.getUnUpdatable() != null) {
                itemBuilder.field("unUpdatable", tableFieldDefinition.getUnUpdatable());
            }
//            if(tableFieldDefinition.getColumnWidth()!=null){
//                itemBuilder.field("columnWidth", tableFieldDefinition.getColumnWidth());
//            }
            if (tableFieldDefinition.getIsDefaultHidden() != null) {
                itemBuilder.field("isDefaultHidden", tableFieldDefinition.getIsDefaultHidden());
            }
            if (tableFieldDefinition.getEditTypeOperateExtend() != null) {
                if (EditType.select.name().equalsIgnoreCase(tableFieldDefinition.getEditType())) {
                    itemBuilder.field("selectOperation", tableFieldDefinition.getEditTypeOperateExtend(), true);
                }
            }
            if (tableFieldDefinition.getExtendsAttributeInfo() != null) {
                itemBuilder.field("extendsAttributeInfo", tableFieldDefinition.getExtendsAttributeInfo(),true);
            } else {
                itemBuilder.field("extendsAttributeInfo", "{}", true);
            }

            itemBuilder.end();
        }
        return tsObjectArrayBuilder.build();
    }

    private static String buildAccessURL(String tableName, String appName) {
        StringBuilder stringBuilder = new StringBuilder();
        String attributeName = StringHelper.transferLine2Humps(tableName, "_", false);
        String initUpperAttributeName = StringHelper.initUpper(attributeName);

        stringBuilder.append("export const batchDeleteUri = '/" + appName + "/" + attributeName + "/batchDelete" + initUpperAttributeName + "List';\n");
        stringBuilder.append("export const fetchListUri = '/" + appName + "/" + attributeName + "/select" + initUpperAttributeName + "List';\n");
        stringBuilder.append("export const fetchDetailUri = '/" + appName + "/" + attributeName + "/select" + initUpperAttributeName + "List';\n");
        stringBuilder.append("export const addUri = '/" + appName + "/" + attributeName + "/save" + initUpperAttributeName + "';\n");
        stringBuilder.append("export const updateUri = '/" + appName + "/" + attributeName + "/save" + initUpperAttributeName + "';\n");
        stringBuilder.append("export const BaseURI = '/" + appName + "';\n");
        stringBuilder.append("export const BaseExcelUri = BaseURI + '/" + "webDisplayServerExcel" + "';\n");
        stringBuilder.append("export const primaryKey = '" + attributeName + "Id';\n");
        stringBuilder.append("export const entityName = '" + attributeName + "';\n");
        return stringBuilder.toString();

    }

}

